package com.coollang.smartbra.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.SportStatisticsPagerAdapter;
import com.coollang.smartbra.base.BaseActivity;
import com.coollang.smartbra.fragment.SportStatisticsFragment;
import com.coollang.smartbra.interfaces.ActionType;
import com.coollang.smartbra.model.SportRecordModel;
import com.coollang.smartbra.presenter.SportStatisticsActivity1Presenter;
import com.coollang.smartbra.utils.ActionTypeParse;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.views.NoScrollViewPager;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.Arrays;
import java.util.List;

/**
 * @author: Timmy
 * @data: 2018/1/30
 * @Fuction:
 */

public class SportStatisticsActivity1 extends BaseActivity implements View.OnClickListener {

    private NoScrollViewPager mViewPager;
    private MagicIndicator mMagicIndicator;
    private static Integer[] ACTION_CODE = {
            ActionType.SDT, ActionType.YWQZ, ActionType.JF, ActionType.FWC, ActionType.PBZC,
            ActionType.TQ, ActionType.ZQ, ActionType.LTQ, ActionType.XFY, ActionType.ZZST, ActionType.JFZYMJ
    };
    private List<Integer> mDataList = Arrays.asList(ACTION_CODE);
    private SportStatisticsPagerAdapter mSportStatisticsAdapter = new SportStatisticsPagerAdapter(getSupportFragmentManager(), mDataList);
    private int mIndex;
    private SportStatisticsActivity1Presenter mPresenter;
    private ProgressDialog mProgressDialog;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_sport_statistics1);

        View statusBar = findViewById(R.id.status_bar);
        LinearLayout content = findViewById(R.id.content);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);

        processIntent();

        findViewById(R.id.iv_back).setOnClickListener(this);
        mViewPager = findViewById(R.id.view_pager);
        mViewPager.setAdapter(mSportStatisticsAdapter);
        mViewPager.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
        mViewPager.addOnPageChangeListener(mOnPageChangeListener);
        mViewPager.setScroll(false);
        initIndicator();
    }

    private void processIntent() {
        mIndex = getIntent().getIntExtra("index", 0);
    }

    private void initIndicator() {
        mMagicIndicator = findViewById(R.id.magic_indicator);
        mMagicIndicator.setBackgroundColor(ContextCompat.getColor(this, R.color.bg_white));
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.GRAY);
                simplePagerTitleView.setSelectedColor(Color.BLACK);

                String actionName = ActionTypeParse.getActionName(mDataList.get(index));

                simplePagerTitleView.setText(actionName);

                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(ContextCompat.getColor(SportStatisticsActivity1.this, R.color.progrss_bg));
                return linePagerIndicator;
            }
        });

        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @Override
    protected void initData() {
        mPresenter = new SportStatisticsActivity1Presenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            default:
                break;
        }
    }


    private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // 加载完成 手动选择加载的项目
            mViewPager.setCurrentItem(mIndex);
            //移除监听
            mViewPager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    };

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mPosition = position;
            showProgressDialog("", "加载数据中...");
            mPresenter.loadHistoryData(ACTION_CODE[position]);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };

    private int mPosition = 0;

    public SportStatisticsFragment getFragment() {
        return mSportStatisticsAdapter.getFragment(mPosition);
    }

    /**
     * 获取到数据,发送到Fragment
     */
    public void updateHistoryData(List<SportRecordModel.DataBean> data) {
        getFragment().setHistoryData(data);
        hideProgressDialog();
    }

    private void showProgressDialog(String title, String message) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(SportStatisticsActivity1.this, title, message, false);
        } else if (mProgressDialog.isShowing()) {
            mProgressDialog.setTitle(title);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.show();
    }

    private void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public void loadDataFailed(String message) {
        hideProgressDialog();
        Toast.makeText(this, "" + message, Toast.LENGTH_SHORT).show();
    }

    public int getActionTypeCode() {
        return ACTION_CODE[mPosition];
    }
}
